package net.telesing.tcmc.back.logic;

import net.telesing.tcmc.back.dao.UserDao;
import net.telesing.tcmc.back.model.OrganizationModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.core.CryptUtil;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;


@IocBean
public class UserLogic
{
    @Inject
    protected UserDao userDao;

    public static final int USER_NOT_FOUND     = 1;
    public static final int PASSWORD_NOT_MATCH = 2;
    public static final int REPEAT_ACCOUNT     = 3;
    public static final int REPEAT_NAME        = 4;
    public static final int REPEAT_IMEI        = 5;
    public static final int REPEAT_UUID        = 6;

    private LogicException error(int code)
    {
        switch (code)
        {
            case USER_NOT_FOUND:
                return new LogicException(code, "用户不存在");
            case PASSWORD_NOT_MATCH:
                return new LogicException(code, "密码错误");
            case REPEAT_ACCOUNT:
                return new LogicException(code, "账号重复");
            case REPEAT_NAME:
                return new LogicException(code, "用户名已存在");
            case REPEAT_IMEI:
                return new LogicException(code, "您还未注册");
            case REPEAT_UUID:
                return new LogicException(code, "您还未注册");
            default:
                return new LogicException(code, "未知错误");
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public UserModel find(long id)
    {
        return userDao.fetch(id);
    }

    /**
     * 添加用户
     *
     * @param user
     * @param creator
     * @return
     * @throws LogicException
     */
    public UserModel create(final UserModel user, final long creator) throws LogicException
    {
        Cnd cnd = Cnd.where("NAME", "=", user.getName()).and("STATE","<>",0);
        if (null != userDao.fetch(UserModel.class, cnd))
            throw error(REPEAT_NAME);
        user.setPass(CryptUtil.MD5(user.getPass()));
        return userDao.insert(user, creator);
    }

    /**
     * 修改用户
     *
     * @param user
     * @param updater
     * @return
     * @throws LogicException
     */
    public UserModel modify(UserModel user, long updater) throws LogicException
    {
        Cnd cnd = Cnd.where("ID", "=", user.getId());
        UserModel userModel = userDao.fetch(cnd);
        if (!user.getName().equals(userModel.getName()))
        {
            Cnd aCnd = Cnd.where("NAME", "=", user.getName()).and("STATE","<>",0);
            if (null != userDao.fetch(UserModel.class, aCnd))
                throw error(REPEAT_NAME);
        }

        if (null == userModel)
            throw error(USER_NOT_FOUND);
        else
        {
            if(!Strings.isEmpty(user.getPass())){
                user.setPass(CryptUtil.MD5(user.getPass()));
            }else {
                user.setPass(userModel.getPass());
            }
            userDao.update(user, updater);
        }
        return userDao.fetch(user.getId());
    }
}
